package ywpt.service.work;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.CommonTable;
import ywpt.model.SysMsgInfo;

public class SysMsgInfoServiceImpl extends HibernateEntityDao<SysMsgInfo> implements SysMsgInfoService{

	@SuppressWarnings("unchecked")
	@Override
	public List<SysMsgInfo> getConferenceInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public SysMsgInfo getSysMsgInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getSysMsgInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	public void saveSysMsgInfo(SysMsgInfo sysMsgInfo, Long msgTypeId,
			Long invalidTypeId,String[] orgId, String[] recive) {
		if (msgTypeId!=null) {
			sysMsgInfo.setMsgType(this.get(CommonTable.class, msgTypeId));
		}
		if (invalidTypeId!=null) {
			sysMsgInfo.setInvalidType(this.get(CommonTable.class, invalidTypeId));
		}
		if(orgId!=null&&orgId.length>0){
			String str="";
			for(int i=0;i<orgId.length;i++){
				str = str + orgId[i]+",";
			}
			sysMsgInfo.setReceiveOrg(str);
		}
		if(recive!=null&&recive.length>0){
			String str="";
			for(int i=0;i<recive.length;i++){
				str = str + recive[i]+",";
			}
			sysMsgInfo.setReceiveUser(str);
		}
		this.save(sysMsgInfo);
		sysMsgInfo.setUrl("/work/sysMsg/edit-"+sysMsgInfo.getId()+".action");
		this.update(sysMsgInfo);
		
	}

	@Override
	public void updateSysMsgInfo(SysMsgInfo sysMsgInfo, Long msgTypeId,
			Long invalidTypeId,String[] orgId, String[] recive) {
		if (msgTypeId!=null) {
			sysMsgInfo.setMsgType(this.get(CommonTable.class, msgTypeId));
		}
		if (invalidTypeId!=null) {
			sysMsgInfo.setInvalidType(this.get(CommonTable.class, invalidTypeId));
		}
		if(orgId!=null&&orgId.length>0){
			String str="";
			for(int i=0;i<orgId.length;i++){
				str = str + orgId[i]+",";
			}
			sysMsgInfo.setReceiveOrg(str);
		}else if(sysMsgInfo.getReceiveOrg()==null){
			//sysMsgInfo.setReceiveOrg(null);
		}
		if(recive!=null&&recive.length>0){
			String str="";
			for(int i=0;i<recive.length;i++){
				str = str + recive[i]+",";
			}
			sysMsgInfo.setReceiveUser(str);
		}else if(sysMsgInfo.getReceiveUser().length()<=0){
			sysMsgInfo.setReceiveUser(null);
		}
		this.update(sysMsgInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysMsgInfo> getSysMsgInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void saveSysMsgInfo(SysMsgInfo sysMsgInfo) {
		this.save(sysMsgInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void invalidMsg(int businessType, Long businessId) {
		List<SysMsgInfo> sysMsgInfoList = new ArrayList<SysMsgInfo>();
		DetachedCriteria query = DetachedCriteria.forClass(SysMsgInfo.class);
		query.add(Restrictions.eq("businessType", businessType));
		query.add(Restrictions.eq("businessId", businessId));
		sysMsgInfoList.addAll(this.findAllByCriteria(query));
		for (SysMsgInfo sysMsgInfo:sysMsgInfoList) {
			sysMsgInfo.setInvalidFlag(true);
			this.update(sysMsgInfo);
		}
			
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysMsgInfo> getMsgs(int businessType, Long businessId) {
		DetachedCriteria query = DetachedCriteria.forClass(SysMsgInfo.class);
		query.add(Restrictions.eq("businessType", businessType));
		query.add(Restrictions.eq("businessId", businessId));
		return this.findAllByCriteria(query);
	}
}
